/**
 *====================================================
 * 文件名称: AbnormalCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月21日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.util.List;

import org.apache.commons.lang.BooleanUtils;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.context.FinanceConstant;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.LogisticsCompanyEntity;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.member.service.LogisticsCompanyService;
import com.yuexunit.finance.project.action.v1.model.AbnormalBlame;
import com.yuexunit.finance.project.action.v1.model.AbnormalCasualty;
import com.yuexunit.finance.project.action.v1.model.AbnormalCorrective;
import com.yuexunit.finance.project.action.v1.model.AbnormalEquipment;
import com.yuexunit.finance.project.action.v1.model.AbnormalExpense;
import com.yuexunit.finance.project.action.v1.model.AbnormalRisk;
import com.yuexunit.finance.project.action.v1.model.AbnormalSurvey;
import com.yuexunit.finance.project.action.v1.model.input.AbnormalUpdateParams;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalBlameDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalCasualtyDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalCorrectiveDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalEquipmentDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalExpenseDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalRiskDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalSurveyDetail;
import com.yuexunit.finance.project.action.v1.model.output.AbnormalTrackDetail;
import com.yuexunit.finance.project.action.v1.model.output.DetainedApplicationDetail;
import com.yuexunit.finance.project.action.v1.model.output.DetainedApplicationLineInfo;
import com.yuexunit.finance.project.entity.AbnormalBlameEntity;
import com.yuexunit.finance.project.entity.AbnormalCasualtyEntity;
import com.yuexunit.finance.project.entity.AbnormalCorrectiveEntity;
import com.yuexunit.finance.project.entity.AbnormalEntity;
import com.yuexunit.finance.project.entity.AbnormalEquipmentEntity;
import com.yuexunit.finance.project.entity.AbnormalExpenseEntity;
import com.yuexunit.finance.project.entity.AbnormalInventoryEntity;
import com.yuexunit.finance.project.entity.AbnormalRiskEntity;
import com.yuexunit.finance.project.entity.AbnormalSurveyEntity;
import com.yuexunit.finance.project.entity.AbnormalTrackEntity;
import com.yuexunit.finance.project.entity.ContainerBulkstockEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventorySignEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryTallyEntity;
import com.yuexunit.finance.project.entity.DetainedApplicationEntity;
import com.yuexunit.finance.project.entity.DetainedInventoryEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.TallyingEntity;
import com.yuexunit.finance.project.entity.lexicon.AbnormalSourceEnum;
import com.yuexunit.finance.project.entity.lexicon.AbnormalTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.ExpenditureEnum;
import com.yuexunit.finance.project.entity.lexicon.OperatorRoleEnum;
import com.yuexunit.finance.project.entity.lexicon.SettlementStateEnum;
import com.yuexunit.finance.project.service.AbnormalBlameService;
import com.yuexunit.finance.project.service.AbnormalCasualtyService;
import com.yuexunit.finance.project.service.AbnormalCorrectiveService;
import com.yuexunit.finance.project.service.AbnormalEquipmentService;
import com.yuexunit.finance.project.service.AbnormalExpenseService;
import com.yuexunit.finance.project.service.AbnormalInventoryService;
import com.yuexunit.finance.project.service.AbnormalRiskService;
import com.yuexunit.finance.project.service.AbnormalSurveyService;
import com.yuexunit.finance.project.service.AbnormalTrackService;
import com.yuexunit.finance.project.service.ContainerBulkstockService;
import com.yuexunit.finance.project.service.ContainerInventoryService;
import com.yuexunit.finance.project.service.ContainerInventoryTallyService;
import com.yuexunit.finance.project.service.DetainedInventoryService;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: AbnormalCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月21日 下午9:46:28
 */
public class AbnormalCastor {

	private static void completeDetainedApplication(DetainedApplicationLineInfo info, DetainedApplicationEntity entity) {
		BeanUtils.copyProperties(entity, info);
		info.setApplicationState(entity.getApplicationState().ordinal());

		DetainedInventoryService detainedInventoryService = ComponentFactory.loadClass(DetainedInventoryService.class);
		List<Long> inventoryIds = detainedInventoryService.inquireGroupDetainedInventoryIds(entity.getDetainedId());
		info.setCaseQuantity(inventoryIds.size());
		if (inventoryIds.isEmpty()) {
			return;
		}
		ContainerBulkstockService containerBulkstockService = ComponentFactory.loadClass(ContainerBulkstockService.class);
		List<ContainerBulkstockEntity> containerBulkstockList = containerBulkstockService.inquireContainerBulkstockList(inventoryIds.toArray(new Long[inventoryIds.size()]));
		BulkstockCastor.fillBulkstockInventories(containerBulkstockList, info.getBulkstockInventories());
	}

	/** 扣货明细 */
	public static DetainedApplicationDetail toDetainedApplicationDetail(DetainedApplicationEntity detainedApplication) {
		DetainedApplicationDetail info = new DetainedApplicationDetail();
		completeDetainedApplication(info, detainedApplication);
		DetainedInventoryService detainedInventoryService = ComponentFactory.loadClass(DetainedInventoryService.class);
		List<DetainedInventoryEntity> detainedInventories = detainedInventoryService.inquireDetainedInventoryList(detainedApplication.getDetainedId());
		for (DetainedInventoryEntity die : detainedInventories) {
			info.getDetainedInventories().add(BulkstockCastor.toDetainedInventory(die));
		}
		return info;
	}

	/** 扣货列表信息 */
	public static DetainedApplicationLineInfo toDetainedApplicationLineInfo(DetainedApplicationEntity detainedApplication) {
		DetainedApplicationLineInfo info = new DetainedApplicationLineInfo();
		completeDetainedApplication(info, detainedApplication);
		return info;
	}

	/** 异常单据 */
	public static AbnormalEntity fromAbnormal(AbnormalUpdateParams params) {
		AbnormalEntity entity = new AbnormalEntity();
		BeanUtils.copyProperties(params, entity, "abnormalType", "handleState");
		entity.setRiskEnabled(BooleanUtils.toBoolean(params.getRiskEnabled()));
		return entity;
	}

	/** 异常调查 */
	public static AbnormalSurveyEntity fromAbnormalSurvey(AbnormalSurvey params) {
		AbnormalSurveyEntity entity = new AbnormalSurveyEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		return entity;
	}

	/** 责任人处置 */
	public static AbnormalBlameEntity fromAbnormalBlame(AbnormalBlame params) {
		AbnormalBlameEntity entity = new AbnormalBlameEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		entity.setExpenditure(ExpenditureEnum.valueOf(params.getExpenditure()));
		if (OperatorRoleEnum.ENTERPRISE == entity.getReceiptType()) {
			EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
			EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getReceiptId());
			entity.setCompany(enterprise.getEnterpriseName());
			entity.setCompanyTenantId(enterprise.getTenantId());
		} else if (OperatorRoleEnum.LOGISTICS == entity.getReceiptType()) {
			LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
			LogisticsCompanyEntity logisticsCompany = logisticsCompanyService.inquireLogisticsCompanyDetail(entity.getReceiptId());
			entity.setCompany(logisticsCompany.getCompanyName());
			entity.setCompanyTenantId(logisticsCompany.getTenantId());
		} else if (OperatorRoleEnum.PLATFORM == entity.getReceiptType()) {
			entity.setCompanyId(null);
			entity.setCompanyTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			entity.setCompanyId(null);
			entity.setCompanyTenantId(null);
		}
		return entity;
	}

	/** 整改总结 */
	public static AbnormalCorrectiveEntity fromAbnormalCorrective(AbnormalCorrective params) {
		AbnormalCorrectiveEntity entity = new AbnormalCorrectiveEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		return entity;
	}

	/** 人员伤亡 */
	public static AbnormalCasualtyEntity fromAbnormalCasualty(AbnormalCasualty params) {
		AbnormalCasualtyEntity entity = new AbnormalCasualtyEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setIndemnity(BooleanUtils.toBoolean(params.getIndemnity()));
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		entity.setExpenditure(ExpenditureEnum.valueOf(params.getExpenditure()));
		if (entity.getIndemnity() != null && entity.getIndemnity()) {
			if (entity.getAbnormalAmount() == null) {
				throw new BusinessException("赔偿金额不允许为空");
			}
		}
		if (OperatorRoleEnum.ENTERPRISE == entity.getReceiptType()) {
			EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
			EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getReceiptId());
			entity.setCompany(enterprise.getEnterpriseName());
			entity.setCompanyTenantId(enterprise.getTenantId());
		} else if (OperatorRoleEnum.LOGISTICS == entity.getReceiptType()) {
			LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
			LogisticsCompanyEntity logisticsCompany = logisticsCompanyService.inquireLogisticsCompanyDetail(entity.getReceiptId());
			entity.setCompany(logisticsCompany.getCompanyName());
			entity.setCompanyTenantId(logisticsCompany.getTenantId());
		} else if (OperatorRoleEnum.PLATFORM == entity.getReceiptType()) {
			entity.setCompanyId(null);
			entity.setCompanyTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			entity.setCompanyId(null);
			entity.setCompanyTenantId(null);
		}
		return entity;
	}

	/** 设备损坏 */
	public static AbnormalEquipmentEntity fromAbnormalEquipment(AbnormalEquipment params) {
		AbnormalEquipmentEntity entity = new AbnormalEquipmentEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		entity.setIndemnity(BooleanUtils.toBoolean(params.getIndemnity()));
		entity.setExpenditure(ExpenditureEnum.valueOf(params.getExpenditure()));
		if (entity.getIndemnity() != null && entity.getIndemnity()) {
			if (entity.getAbnormalAmount() == null) {
				throw new BusinessException("赔偿金额不允许为空");
			}
		}
		if (OperatorRoleEnum.ENTERPRISE == entity.getReceiptType()) {
			EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
			EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getReceiptId());
			entity.setCompany(enterprise.getEnterpriseName());
			entity.setCompanyTenantId(enterprise.getTenantId());
		} else if (OperatorRoleEnum.LOGISTICS == entity.getReceiptType()) {
			LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
			LogisticsCompanyEntity logisticsCompany = logisticsCompanyService.inquireLogisticsCompanyDetail(entity.getReceiptId());
			entity.setCompany(logisticsCompany.getCompanyName());
			entity.setCompanyTenantId(logisticsCompany.getTenantId());
		} else if (OperatorRoleEnum.PLATFORM == entity.getReceiptType()) {
			entity.setCompanyId(null);
			entity.setCompanyTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			entity.setCompanyId(null);
			entity.setCompanyTenantId(null);
		}
		return entity;
	}

	/** 风险控制 */
	public static AbnormalRiskEntity fromAbnormalRisk(AbnormalRisk params) {
		AbnormalRiskEntity entity = new AbnormalRiskEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setSettlementState(SettlementStateEnum.UNKNOWN);
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		return entity;
	}

	/** 异常费用 */
	public static AbnormalExpenseEntity fromAbnormalExpense(AbnormalExpense params) {
		AbnormalExpenseEntity entity = new AbnormalExpenseEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setReportor(FinanceSessionContext.getAccountFullName());
		entity.setExpenditure(ExpenditureEnum.valueOf(params.getExpenditure()));
		entity.setAbnormalSource(AbnormalSourceEnum.UNKNOWN);
		entity.setReceiptType(OperatorRoleEnum.valueOf(params.getReceiptType()));
		if (OperatorRoleEnum.ENTERPRISE == entity.getReceiptType()) {
			EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
			EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getReceiptId());
			entity.setReceiptName(enterprise.getEnterpriseName());
			entity.setReceiptTenantId(enterprise.getTenantId());
		} else if (OperatorRoleEnum.LOGISTICS == entity.getReceiptType()) {
			LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
			LogisticsCompanyEntity logisticsCompany = logisticsCompanyService.inquireLogisticsCompanyDetail(entity.getReceiptId());
			entity.setReceiptName(logisticsCompany.getCompanyName());
			entity.setReceiptTenantId(logisticsCompany.getTenantId());
		} else if (OperatorRoleEnum.PLATFORM == entity.getReceiptType()) {
			entity.setReceiptId(null);
			entity.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			entity.setReceiptId(null);
			entity.setReceiptTenantId(null);
		}
		entity.setDisbursementType(OperatorRoleEnum.valueOf(params.getDisbursementType()));
		if (OperatorRoleEnum.ENTERPRISE == entity.getDisbursementType()) {
			EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
			EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getReceiptId());
			entity.setDisbursementName(enterprise.getEnterpriseName());
			entity.setDisbursementTenantId(enterprise.getTenantId());
		} else if (OperatorRoleEnum.LOGISTICS == entity.getDisbursementType()) {
			LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
			LogisticsCompanyEntity logisticsCompany = logisticsCompanyService.inquireLogisticsCompanyDetail(entity.getReceiptId());
			entity.setDisbursementName(logisticsCompany.getCompanyName());
			entity.setDisbursementTenantId(logisticsCompany.getTenantId());
		} else if (OperatorRoleEnum.PLATFORM == entity.getDisbursementType()) {
			entity.setDisbursementId(null);
			entity.setDisbursementTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			entity.setDisbursementId(null);
			entity.setDisbursementTenantId(null);
		}
		return entity;
	}

	private static void completeAbnormal(AbnormalLineInfo info, AbnormalEntity entity) {
		BeanUtils.copyProperties(entity, info);
		info.setAbnormalState(entity.getAbnormalState().ordinal());
		info.setAbnormalType(entity.getAbnormalType().ordinal());
		info.setHandleState(entity.getHandleState().ordinal());
	}

	/** 异常调查 */
	public static AbnormalSurveyDetail toAbnormalSurvey(AbnormalSurveyEntity entity) {
		AbnormalSurveyDetail info = new AbnormalSurveyDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 责任人处置 */
	public static AbnormalBlameDetail toAbnormalBlame(AbnormalBlameEntity entity) {
		AbnormalBlameDetail info = new AbnormalBlameDetail();
		BeanUtils.copyProperties(entity, info);
		info.setExpenditure(entity.getExpenditure().ordinal());
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 整改总结 */
	public static AbnormalCorrectiveDetail toAbnormalCorrective(AbnormalCorrectiveEntity entity) {
		AbnormalCorrectiveDetail info = new AbnormalCorrectiveDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 人员伤亡 */
	public static AbnormalCasualtyDetail toAbnormalCasualty(AbnormalCasualtyEntity entity) {
		AbnormalCasualtyDetail info = new AbnormalCasualtyDetail();
		BeanUtils.copyProperties(entity, info);
		info.setExpenditure(entity.getExpenditure().ordinal());
		info.setIndemnity(BooleanUtils.toIntegerObject(entity.getIndemnity()));
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 设备损坏 */
	public static AbnormalEquipmentDetail toAbnormalEquipment(AbnormalEquipmentEntity entity) {
		AbnormalEquipmentDetail info = new AbnormalEquipmentDetail();
		BeanUtils.copyProperties(entity, info);
		info.setExpenditure(entity.getExpenditure().ordinal());
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 风险控制 */
	public static AbnormalRiskDetail toAbnormalRisk(AbnormalRiskEntity entity) {
		AbnormalRiskDetail info = new AbnormalRiskDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		info.setSettlementState(entity.getSettlementState() == null ? 0 : entity.getSettlementState().ordinal());
		return info;
	}

	/** 异常跟踪 */
	public static AbnormalTrackDetail toAbnormalTrack(AbnormalTrackEntity entity) {
		AbnormalTrackDetail info = new AbnormalTrackDetail();
		BeanUtils.copyProperties(entity, info);
		return info;
	}

	/** 异常费用 */
	public static AbnormalExpenseDetail toAbnormalExpense(AbnormalExpenseEntity entity) {
		AbnormalExpenseDetail info = new AbnormalExpenseDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAbnormalSource(entity.getAbnormalSource().ordinal());
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		info.setExpenditure(entity.getExpenditure().ordinal());
		info.setReceiptType(entity.getReceiptType().ordinal());
		info.setDisbursementType(entity.getDisbursementType().ordinal());
		return info;
	}

	/** 异常信息明细 */
	public static AbnormalDetail toAbnormalDetail(AbnormalEntity entity) {
		AbnormalDetail info = new AbnormalDetail();
		completeAbnormal(info, entity);
		Long abnormalId = entity.getAbnormalId();
		AbnormalSurveyService abnormalSurveyService = ComponentFactory.loadClass(AbnormalSurveyService.class);
		List<AbnormalSurveyEntity> surveys = abnormalSurveyService.inquireAbnormalSurveyList(abnormalId);
		for (AbnormalSurveyEntity e : surveys) {
			info.getAbnormalSurveys().add(toAbnormalSurvey(e));
		}
		AbnormalRiskService abnormalRiskService = ComponentFactory.loadClass(AbnormalRiskService.class);
		List<AbnormalRiskEntity> risks = abnormalRiskService.inquireAbnormalRiskList(abnormalId);
		for (AbnormalRiskEntity e : risks) {
			info.getAbnormalRisks().add(toAbnormalRisk(e));
		}
		AbnormalBlameService abnormalBlameService = ComponentFactory.loadClass(AbnormalBlameService.class);
		List<AbnormalBlameEntity> blames = abnormalBlameService.inquireAbnormalBlameList(abnormalId);
		for (AbnormalBlameEntity e : blames) {
			info.getAbnormalBlames().add(toAbnormalBlame(e));
		}
		AbnormalCorrectiveService abnormalCorrectiveService = ComponentFactory.loadClass(AbnormalCorrectiveService.class);
		List<AbnormalCorrectiveEntity> correctives = abnormalCorrectiveService.inquireAbnormalCorrectiveList(abnormalId);
		for (AbnormalCorrectiveEntity e : correctives) {
			info.getAbnormalCorrectives().add(toAbnormalCorrective(e));
		}
		AbnormalCasualtyService abnormalCasualtyService = ComponentFactory.loadClass(AbnormalCasualtyService.class);
		List<AbnormalCasualtyEntity> casualties = abnormalCasualtyService.inquireAbnormalCasualtyList(abnormalId);
		for (AbnormalCasualtyEntity e : casualties) {
			info.getAbnormalCasualties().add(toAbnormalCasualty(e));
		}
		AbnormalEquipmentService abnormalEquipmentService = ComponentFactory.loadClass(AbnormalEquipmentService.class);
		List<AbnormalEquipmentEntity> equipments = abnormalEquipmentService.inquireAbnormalEquipmentList(abnormalId);
		for (AbnormalEquipmentEntity e : equipments) {
			info.getAbnormalEquipments().add(toAbnormalEquipment(e));
		}
		AbnormalTrackService abnormalTrackService = ComponentFactory.loadClass(AbnormalTrackService.class);
		List<AbnormalTrackEntity> tracks = abnormalTrackService.inquireAbnormalTrackList(abnormalId);
		for (AbnormalTrackEntity e : tracks) {
			info.getAbnormalTracks().add(toAbnormalTrack(e));
		}
		AbnormalExpenseService abnormalExpenseService = ComponentFactory.loadClass(AbnormalExpenseService.class);
		List<AbnormalExpenseEntity> expenses = abnormalExpenseService.inquireAbnormalExpenseList(abnormalId);
		for (AbnormalExpenseEntity e : expenses) {
			info.getAbnormalExpenses().add(toAbnormalExpense(e));
		}
		AbnormalInventoryService abnormalInventoryService = ComponentFactory.loadClass(AbnormalInventoryService.class);
		List<AbnormalInventoryEntity> inventories = abnormalInventoryService.inquireAbnormalInventoryList(abnormalId);
		for (AbnormalInventoryEntity e : inventories) {
			info.getAbnormalInventories().add(BulkstockCastor.toAbnormalInventory(e));
		}
		return info;
	}

	/** 异常信息列表 */
	public static AbnormalLineInfo toAbnormalLineInfo(AbnormalEntity entity) {
		AbnormalLineInfo info = new AbnormalLineInfo();
		completeAbnormal(info, entity);
		return info;
	}

	/** 理货监管-异常信息 */
	public static AbnormalEntity fromTally(TallyingEntity tallying, Long[] inventoryIds) {
		ContainerInventoryService containerInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);
		ContainerInventoryEntity inventory = containerInventoryService.inquireContainerInventory(inventoryIds[0]);
		ContainerInventoryTallyService containerInventoryTallyService = ComponentFactory.loadClass(ContainerInventoryTallyService.class);
		ContainerInventoryTallyEntity inventoryTally = containerInventoryTallyService.inquireContainerInventoryTally(inventoryIds[0]);

		AbnormalEntity entity = new AbnormalEntity();
		entity.setAbnormalSourceId(tallying.getTallyingId());
		entity.setAbnormalType(AbnormalTypeEnum.TALLY);
		entity.setAbnormalState(inventory.getAbnormalState());
		entity.setFinanceId(tallying.getFinanceId());
		entity.setFinanceNumber(tallying.getFinanceNumber());
		entity.setHappenDate(DateUtil.formatDate(inventory.getTallyDateTime()));
		entity.setHappenAddress(inventoryTally.getTallyAddress());
		entity.setEnterpriseId(inventory.getEnterpriseId());
		entity.setEnterpriseName(inventory.getEnterpriseName());
		entity.setQuantity(inventoryIds.length);
		entity.setReportDate(DateUtil.nowDate());
		entity.setReportDepartment(tallying.getLogisticsCompanyName());
		entity.setReportor(tallying.getClerkName());
		return entity;
	}

	/** 配送签收-异常信息 */
	public static AbnormalEntity fromDeliveryInventory(ContainerInventorySignEntity sign, ContainerInventoryEntity inventory) {
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceApplicationEntity financeApplication = financeApplicationService.inquireFinanceApplicationEntity(inventory.getFinanceId());

		LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
		LogisticsCompanyEntity company = logisticsCompanyService.inquireLogisticsCompanyDetail(inventory.getDeliveryCompanyId());

		AbnormalEntity entity = new AbnormalEntity();
		entity.setAbnormalSourceId(inventory.getInventoryId());
		entity.setAbnormalType(AbnormalTypeEnum.SIGN);
		entity.setAbnormalState(inventory.getAbnormalState());
		entity.setFinanceId(inventory.getFinanceId());
		entity.setFinanceNumber(financeApplication.getFinanceNumber());
		entity.setHappenDate(DateUtil.formatDate(inventory.getSignDateTime()));
		entity.setEnterpriseId(inventory.getEnterpriseId());
		entity.setEnterpriseName(inventory.getEnterpriseName());
		entity.setQuantity(1);
		entity.setReportDate(DateUtil.nowDate());
		entity.setReportDepartment(company.getCompanyName());
		if (sign != null) {
			entity.setHappenAddress(sign.getSignAddress());
			entity.setReportor(sign.getSignAccountor());
		} else {
			entity.setReportor(FinanceSessionContext.getAccountFullName());
		}
		return entity;
	}

	/** 扣货审批通过-异常信息 */
	public static AbnormalEntity fromDetainedApplication(DetainedApplicationEntity detained, List<ContainerInventoryEntity> inventories) {
		ContainerInventoryEntity inventory = inventories.get(0);

		AbnormalEntity entity = new AbnormalEntity();
		entity.setAbnormalSourceId(inventory.getInventoryId());
		entity.setAbnormalType(AbnormalTypeEnum.SIGN);
		entity.setAbnormalState(inventory.getAbnormalState());
		entity.setFinanceId(inventory.getFinanceId());
		entity.setFinanceNumber(detained.getFinanceNumber());
		entity.setHappenDate(DateUtil.formatDate(detained.getApplicantDateTime()));
		entity.setHappenAddress(null);
		entity.setEnterpriseId(inventory.getEnterpriseId());
		entity.setEnterpriseName(inventory.getEnterpriseName());
		entity.setQuantity(inventories.size());
		entity.setReportDate(DateUtil.nowDate());
		entity.setReportDepartment(detained.getApplicantDepartment());
		entity.setReportor(detained.getApplicant());
		return entity;
	}

}
